<template>
  <view>
    <!-- 搜索组件 -->
    <view class="search-box">
        <uni-search-bar :radius="100" @input="input" placeholder="请输入搜索内容" cancelButton="none" clearButton="always" focus="true"></uni-search-bar>
    </view>
    
    <!-- 搜索建议列表 -->
    <view class="sugg-list"  style="background-color: #fff;" v-if="searchResults.length!==0">
       <view class="sugg-item" v-for="(sugg,s1) in searchResults" :key="s1" @click="gotoDetail(sugg)">
          <view class="goods-name">
            {{sugg.goods_name}}
          </view>
          <uni-icons type="forward" size="30"></uni-icons>
       </view>
    </view>
  
    <!-- 搜索历史 -->
    <view class="history-box" v-if="searchResults.length==0">
       <!-- 标题区域 -->
       <view class="history-title">
         <text>搜索历史</text>
         <uni-icons type="trash-filled" size="20" @click="searchClearn"></uni-icons>
       </view>
       <!-- 列表区域 -->
       <view class="history-list">
          <uni-tag :text="his" v-for="(his,h1) in historys" :key="h1" @click="gotoGoodList(his)"></uni-tag>
       </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer:null,
        kw:'',
        // 搜索结果列表
            searchResults: [],
         //搜索历史的数组
            historyList:[]
      }
    },
    computed:{
       historys(){
         return [...this.historyList].reverse()
       }
    },
    methods: {
      //input输入事件处理函数
      input(e){
        clearTimeout(this.timer)
       this.timer = setTimeout(()=>{
          this.kw = e;
          this.getSearchResults()
       },500)
      },
     async getSearchResults(){
        //判断搜索结果是否为空
        // 清除掉空格
        this.kw.trim()
        if(this.kw === ''){
          
          this.searchResults=[]
          return
        }
        
      const {data:res} = await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
        if (res.meta.status !== 200) return uni.$showMsg()
          this.searchResults = res.message
          this.searchHistory()
      },
      gotoDetail(sugg){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?gid=' + sugg.goods_id
        })
      },
      // 搜索历史展示
      searchHistory(){
        // this.historyList.push(this.kw)
        const set = new Set(this.historyList)
        set.delete(this.kw)
         set.add(this.kw)
        this.historyList = Array.from(set)
        //本地持久化存储
        uni.setStorageSync('kw',JSON.stringify(this.historyList))
      },
      // 历史搜索清除
      searchClearn(){
        this.historyList = []
        uni.setStorageSync('kw',[])
      },
      //历史tag标签跳转
      gotoGoodList(his){
        uni.navigateTo({
          url:'/subpkg/good_list/good_list?query=' + his  
        })
      }
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kw')|| [])
    }
  }
</script>

<style lang="scss">
.search-box{
  position: sticky;
  top:0;
  z-index: 999;
}

.sugg-list{
  padding: 0 5px;
  .sugg-item{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 13px 0;
    border-bottom: 2px solid #F3F3F3;
    .goods-name{
      padding: 0 10px;
       white-space: nowrap;
       overflow: hidden;
       text-overflow: ellipsis;
    }
  }
}

.history-box{
  padding: 0 5px;
  .history-title{
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    padding: 0 10px;
    border-bottom: 1px solid #fff;
    font-size: 13px;
  }
  .history-list{
    display: flex;
    flex-wrap: wrap;
    margin-top: 5px;
    .uni-tag{
      display: block;
      margin: 5px;
      padding: 3px 15px;
      background-color: #fff;
      color: deepskyblue;
      text-align: center;
      line-height: 30px;
    }
  }
}
</style>
